package com.example.smartcity_poverty.ui.AllService.PetHospital.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.view.MenuItem
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.PetHospitalBean.JsThisAllModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_js_this_add_true.*
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject

/**
 * 提问
 */
class JsThisAddTrueActivity : AppCompatActivity() {

    private val rows by lazy { intent.getSerializableExtra("rows") as JsThisAllModel.RowsBean }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_js_this_add_true)
        title = "提问"

        // 填充数据
        glide(url + rows.avatar, tJsMeShowImage)
        tJsMeShowName.text = rows.name
        tJsMeShowTv1.text = "医生职称: ${rows.jobName}"
        tJsMeShowTv2.text = "执业编号: ${rows.practiceNo}"


        // 问题提交
        tJsMeShowOk.setOnClickListener {
            if (tJsMeShowEditText.text.toString().isNotEmpty()) {
                coroutine() {
                    api.getJsAddUserList(getToken(), JSONObject().apply {
                        put("doctorId", rows.id.toString())
                        put("question", tJsMeShowEditText.text.toString())
                    }.toString().toRequestBody("application/json".toMediaTypeOrNull())).apply {
                        runOnUiThread {
                            this.msg.show()
                            if (this.code == "200") {
                                runOnUiThread {
                                    finish()
                                }
                            }
                        }
                    }
                }
            } else "请先登录！".show()
        }


        // 点击选择照片
        tJsMeShowImageIs.setOnClickListener {
            val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
            startActivityForResult(intent, 10)
        }

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 10 && resultCode == Activity.RESULT_OK) {
            val uri = data?.data
            tJsMeShowImageIs.setImageURI(uri)
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}